//
//  TouSuDetailViewController.m
//  网络货运PRO版
//
//  Created 网络货运PRO版 on 2019/12/23.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "TouSuDetailViewController.h"
#import "XMTextView.h"

@interface TouSuDetailViewController ()
{
    NSString *_abnormalFeedback;
}
@property(strong, nonatomic)UIScrollView *scrollView;

@property(strong, nonatomic)NSDictionary *reasonDic;

@property(strong, nonatomic)XMTextView *textView;//处理结果反馈

@end

@implementation TouSuDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _abnormalFeedback = @"";
    
    [self setupBaseView];
}

- (void)setupBaseView {
    
    self.navigationItem.title = @"投诉详情";
    
    self.view.backgroundColor = BackgroundColor;
    
    [self tousuDetailRequest];

}

- (void)tousuDetailRequest {
    
    WEAKSELF
        
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@/%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], ORDER_DETAIL_ABNORMAL ,_touSuRecordsCode) withParameter:@{@"token":[UserDefaultUtil valueForKey:USER_TOKEN],} withVC:self netName:@"投诉信息详情" success:^(id response, NSString *msg) {
        
            _reasonDic = (NSDictionary *)response;

            [weakSelf mainUILayout];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

- (void)mainUILayout {
    
    //上方信息视图
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 180)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:topView];
    
    NSArray *titles = @[@"投诉人",@"发生地点",@"发生时间",@"关联单号"];
    NSMutableArray *valueArr = [[NSMutableArray alloc] init];
    
    if (_abnormalFrom == 0) {
        //投诉我的
        [valueArr addObject:[NSString stringWithFormat:@"%@（%@）", [NSString nullToString:CJStringWithFormat(@"%@", _reasonDic[@"shipmentName"])], [NSString nullToString:CJStringWithFormat(@"%@", _reasonDic[@"shipmentPhone"])]]];
    } else {
        //我投诉的
        [valueArr addObject:[NSString stringWithFormat:@"%@（%@）", [NSString nullToString:CJStringWithFormat(@"%@", _reasonDic[@"driverName"])], [NSString nullToString:CJStringWithFormat(@"%@", _reasonDic[@"driverPhone"])]]];
    }
    

    [valueArr addObject:[NSString nullToString:_reasonDic[@"address"]]];
    [valueArr addObject:[NSString nullToString:_reasonDic[@"occurrenceTime"]]];
    [valueArr addObject:[NSString nullToString:_reasonDic[@"orderCode"]]];

    
    for (int i=0; i<titles.count; i++) {
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 45*i, 100, 45)];
        textLabel.font = font(14);
        textLabel.text = titles[i];
        textLabel.textColor = Light_TextColor;
        [topView addSubview:textLabel];
        
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(topView.cy_width-15-250, 45*i, 250, 45)];
        valueLabel.font = font(14);
        valueLabel.text = valueArr[i];
        valueLabel.numberOfLines = 2;
        valueLabel.textColor = Dark_TextColor;
        valueLabel.adjustsFontSizeToFitWidth = YES;
        valueLabel.textAlignment = NSTextAlignmentRight;
        [topView addSubview:valueLabel];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, 45*(i+1), ScreenW-30, 1)];
        line.backgroundColor = BackgroundColor;
        [topView addSubview:line];
    }
    
    //投诉原因视图
    UIView *reasonView = [[UIView alloc] initWithFrame:CGRectMake(0, topView.cy_bottom + 10, ScreenW, 200)];
    reasonView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:reasonView];
    
    UILabel *reasonTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 200, 25)];
    reasonTextLabel.font = font(14);
    reasonTextLabel.text = @"投诉原因";
    reasonTextLabel.textColor = Light_TextColor;
    [reasonView addSubview:reasonTextLabel];
    
    CGSize size = [DCSpeedy dc_calculateTextSizeWithText:CJStringWithFormat(@"%@:%@", _reasonDic[@"abnormalTypeName"], _reasonDic[@"abnormalRemark"]) WithTextFont:14 WithMaxW:ScreenW-30];
    reasonView.cy_height = 40 + size.height + 20;
    
    UILabel *reasonValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, reasonTextLabel.bottom, ScreenW-30, 40)];
    reasonValueLabel.font = font(14);
    reasonValueLabel.numberOfLines = 2;
    //    reasonValueLabel.textAlignment = NSTextAlignmentRight;
    reasonValueLabel.text = CJStringWithFormat(@"%@:%@", _reasonDic[@"abnormalTypeName"], _reasonDic[@"abnormalRemark"]);
    reasonValueLabel.textColor = Dark_TextColor;
    [reasonView addSubview:reasonValueLabel];
    
    CGFloat bottomViewOffsetY = reasonView.cy_bottom + 10;

    NSArray *imageArray = [DCSpeedy isBlankString:_reasonDic[@"abnormalImage"]] ? [[NSArray alloc] init] : [_reasonDic[@"abnormalImage"] componentsSeparatedByString:@","];
    //        NSArray *imageArray = @[@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",@"https://ntocc-test.obs.cn-north-4.myhuaweicloud.com:443/2019-12-19/839d53ea79a140c7889561005f7a4393.jpeg",];
    if (imageArray.count > 0) {
        //图片背景
        UIView *imagesView = [[UIView alloc] initWithFrame:CGRectMake(0, reasonView.cy_bottom + 10, ScreenW, 200)];
        imagesView.backgroundColor = [UIColor whiteColor];
        [self.scrollView addSubview:imagesView];
        
        float width = (ScreenW-75)/4;
        
        imagesView.cy_height = imageArray.count == 0 ? 0 : (20 + (imageArray.count/4+1)*width + imageArray.count/4*10);
        
        for (int i=0; i<imageArray.count; i++) {
            
            int x = i%4;
            int y = i/4;
            
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x*width+(15*(x+1)), y*width + (10*(y+1)), width, width)];
            setNetImage(image, imageArray[i], @"gcy_icon_plaholder");
            image.userInteractionEnabled = YES;
            [imagesView addSubview:image];
            
            [image nyl_addTapGes:^{
                HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
                browser.isFullWidthForLandScape = YES;
                browser.isNeedLandscape = YES;
                browser.currentImageIndex = i;
                browser.imageArray = imageArray;
                [browser show];
            }];
        }
        
        bottomViewOffsetY = imagesView.cy_bottom + 10;
    }
    
    
    //最下面信息
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, bottomViewOffsetY, ScreenW, 140)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:bottomView];
    
    UILabel *statusTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 120, 45)];
    statusTextLabel.font = font(14);
    statusTextLabel.text = @"处理状态";
    statusTextLabel.textColor = Light_TextColor;
    [bottomView addSubview:statusTextLabel];
    
    UILabel *statusValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenW-15-100, 0, 100, 45)];
    statusValueLabel.font = font(14);
    statusValueLabel.text = [_reasonDic[@"isHandle"] intValue] == 0 ? @"未处理" : @"已处理";
    statusValueLabel.textColor = Dark_TextColor;
    statusValueLabel.textAlignment = NSTextAlignmentRight;
    [bottomView addSubview:statusValueLabel];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, statusValueLabel.cy_bottom, ScreenW, 10)];
    line.backgroundColor = BackgroundColor;
    [bottomView addSubview:line];
    
    UILabel *resultTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, line.cy_bottom, 200, 40)];
    resultTextLabel.font = font(16);
    resultTextLabel.text = @"处理结果";
    resultTextLabel.textColor = Light_TextColor;
    [bottomView addSubview:resultTextLabel];
    
    UILabel *resultValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, resultTextLabel.cy_bottom, ScreenW-30, 50)];
    resultValueLabel.font = font(14);
    resultValueLabel.numberOfLines = 0;
    resultTextLabel.adjustsFontSizeToFitWidth = YES;
    resultValueLabel.text = [DCSpeedy isBlankString:_reasonDic[@"handleIdea"]] == YES ? @"暂无" : _reasonDic[@"handleIdea"];
    resultValueLabel.textColor = Dark_TextColor;
    [bottomView addSubview:resultValueLabel];
    
    if ([_reasonDic[@"isHandle"] intValue] == 1) {
        
        if ([_reasonDic[@"satisfy"] integerValue] != 0) {
            //已经有了满意 不满意的 操作
            UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, bottomView.cy_bottom + 10, ScreenW, 130)];
            bgView.backgroundColor = [UIColor whiteColor];
            [self.scrollView addSubview:bgView];
            
            UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 45)];
            textLabel.font = font(14);
            textLabel.text = @"满意度";
            textLabel.textColor = Dark_TextColor;
            [bgView addSubview:textLabel];
            
            UILabel *statusValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenW-15-100, 0, 100, 45)];
            statusValueLabel.font = font(14);
            //satisfy (integer, optional): 满意度：1满意，2不满意 ,
            statusValueLabel.text = [_reasonDic[@"satisfy"] intValue] == 1 ? @"满意" : @"不满意";
            statusValueLabel.textColor = Red_Color;
            statusValueLabel.textAlignment = NSTextAlignmentRight;
            [bgView addSubview:statusValueLabel];
            
            //天津二期新增
            UILabel *tsFedbktextLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, statusValueLabel.bottom, 100, 30)];
            tsFedbktextLabel.font = font(14);
            tsFedbktextLabel.text = @"处理结果反馈";
            tsFedbktextLabel.textColor = Dark_TextColor;
            [bgView addSubview:tsFedbktextLabel];
            
            UILabel *tsFedLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, tsFedbktextLabel.bottom, ScreenW-30, 55)];
            tsFedLabel.font = font(14);
            tsFedLabel.text = [NSString nullToString:_reasonDic[@"abnormalFeedback"]];
            tsFedLabel.textColor = Light_TextColor;
            tsFedLabel.numberOfLines = 2;
            [bgView addSubview:tsFedLabel];
            
            self.scrollView.contentSize = CGSizeMake(ScreenW, bgView.cy_bottom + 10);
            
            return;
        }
        
        
        if (_abnormalFrom != 0) {
            //已处理展示 处理结果是否同意
            UIView *resultView = [[UIView alloc] initWithFrame:CGRectMake(0, bottomView.bottom + 10, ScreenW, 185)];
            resultView.backgroundColor = [UIColor whiteColor];
            [self.scrollView addSubview:resultView];
            
            UILabel *feedbackTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 40)];
            feedbackTextLabel.font = font(14);
            feedbackTextLabel.text = @"处理结果反馈";
            feedbackTextLabel.textColor = Light_TextColor;
            [resultView addSubview:feedbackTextLabel];
            
            _textView = [[XMTextView alloc] initWithFrame:CGRectMake(15, feedbackTextLabel.bottom, ScreenW - 30, 80)];
            cutBorder(_textView, 1, @"#96A0B9");
            cut(_textView, 5);
            _textView.textViewListening = ^(NSString *textViewStr) {
                _abnormalFeedback = textViewStr;
            };
            _textView.placeholder = @"请填写处理结果反馈";
            [resultView addSubview:_textView];
            
            NSArray *titles = @[@"满意", @"不满意"];
            CGFloat btnWidth = (ScreenW-60)/2;
            
            for (int i=0; i<titles.count; i++) {
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.tag = 100 + i;
                cut(button, BTN_BORDER_RADIUS)
                [button setTitle:titles[i] forState:UIControlStateNormal];
                button.titleLabel.font = font(17);
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                button.frame = CGRectMake(15 + (btnWidth+30) * i , _textView.bottom + 10, btnWidth, 45);
                [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
                [resultView addSubview:button];
                if (i==0) {
                    [button setBackgroundColor:Theme_ColorValue];
                } else {
                    [button setBackgroundColor:Red_TextColor];
                }
            }
            
            self.scrollView.contentSize = CGSizeMake(ScreenW, resultView.cy_bottom + 20);

        }
        
    } else {
        
        self.scrollView.contentSize = CGSizeMake(ScreenW, bottomView.cy_bottom + 20);
    }
    
}

- (void)buttonClick:(UIButton *)button {
    WEAKSELF
    
    if ([DCSpeedy isBlankString:_abnormalFeedback] == YES) {
        [XSInfoView showInfo:@"处理结果反馈不能为空！" onView:self.view];
        return;
    }
    
    NSDictionary *parmas = @{
        @"code": _reasonDic[@"code"],
        @"abnormalFeedback": _abnormalFeedback,//天津二期新增字段
        @"satisfy": [NSString stringWithFormat:@"%ld", button.tag-99],
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    
    [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], ORDERDETAILABNORMAL_SATISFYCONFIRM) withParameter:parmas withVC:self netName:@"投诉异常满意度确认" success:^(id response, NSString *msg) {
        
        [XSInfoView showInfo:msg onView:weakSelf.view];
                            
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

#pragma mark - lazyLoad
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - 10)];
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}



@end
